package com.devilvan.betrice.qzone.controller;

import com.alibaba.fastjson.JSONObject;
import com.devilvan.betrice.common.BetriceResultCode;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.qzone.dto.QzoneAlbumHeaderDto;
import com.devilvan.betrice.qzone.dto.QzoneDownloadPictureDto;
import com.devilvan.betrice.qzone.po.QzoneAlbumHeaderPo;
import com.devilvan.betrice.qzone.service.QzoneAlbumService;
import com.devilvan.betrice.qzone.vo.QzoneAlbumHeaderVo;
import com.devilvan.betrice.qzone.vo.QzoneDownloadPictureVo;
import com.devilvan.betrice.util.UCopy;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Evad.Wu
 * @Description QQ空间 用户相册 控制层
 * @date 2022-09-11
 */
@Slf4j
@RestController
@RequestMapping(value = "qZoneAlbumController")
@Tag(name = "QQ空间 用户相册 控制层")
public class QzoneAlbumController {
    @Resource(name = "qzoneAlbumServiceImpl")
    private QzoneAlbumService qzoneAlbumService;

    /**
     * 手动录入QQ空间用户相册信息，并保存
     *
     * @param request 录入的相册信息报文
     * @return 保存后的结果集
     */
    @Operation(summary = "手动录入QQ空间用户相册信息，并保存")
    @GetMapping(value = "doQzoneAlbum")
    public BetriceResultVo<List<QzoneAlbumHeaderPo>> doQzoneAlbum(@NonNull @RequestBody String request) {
        BetriceResultVo<List<QzoneAlbumHeaderPo>> betriceResultVo = new BetriceResultVo<>(false);
        try {
            List<QzoneAlbumHeaderVo> qzoneAlbumHeaderVoList = JSONObject.parseObject(request)
                    .getJSONObject("data")
                    .getJSONArray("photos")
                    .toJavaList(QzoneAlbumHeaderVo.class);
            List<QzoneAlbumHeaderDto> qzoneAlbumHeaderDtoList = UCopy.fullCopyCollections(qzoneAlbumHeaderVoList, QzoneAlbumHeaderDto.class);
            List<QzoneAlbumHeaderPo> qzoneAlbumHeaderPoList = qzoneAlbumService.saveUpdateBatch(qzoneAlbumHeaderDtoList);
            betriceResultVo.setSuccess(true);
            betriceResultVo.setData(qzoneAlbumHeaderPoList);
            betriceResultVo.result(BetriceResultCode.SUCCESS_200.getCode(), "操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            betriceResultVo.result(BetriceResultCode.FAILED_500.getCode(), "操作失败：" + e);
        }
        return betriceResultVo;
    }

    /**
     * 下载已保存的相册中的图片
     *
     * @param request 图片下载的存放信息
     */
    @Operation(summary = "下载已保存的相册中的图片")
    @GetMapping(value = "doDownloadPhtoto")
    public void doDownloadPhtoto(@NonNull @RequestBody String request) {
        QzoneDownloadPictureVo downloadPictureVo = JSONObject.parseObject(request, QzoneDownloadPictureVo.class);
        QzoneDownloadPictureDto qzoneDownloadPictureDto = new QzoneDownloadPictureDto();
        UCopy.fullCopy(downloadPictureVo, qzoneDownloadPictureDto);
        qzoneAlbumService.downloadPhoto(qzoneDownloadPictureDto);
    }
}
